//
//  DishOperView.m
//  XRmenu
//
//  Created by loufq on 11-3-24.
//  Copyright 2011 E0571. All rights reserved.
//

#import "DishOperView.h"
#import <CoreText/CoreText.h>

@interface DishOperView(Private)
- (void)drawPathWithArc:(CGFloat)arc;
@end

@implementation DishOperView
@synthesize delegate;
@synthesize item;
@synthesize type;

-(void)dealloc
{
	[item release];
    item = nil;
	delegate =nil;	
	[super dealloc];
}

-(id) initWithConfig:(NSDictionary *)cfg type:(DishOperViewType)aType item:(Product*)aItem
{ 
	self = [super initWithConfig:cfg];
	if (self) {  
		self.item = aItem;
		NSString* rPath = @"";
		
		
		[self add1fingerTap];
		self.clipsToBounds =YES;
		//增加操作按钮
		shapeLayer_ = [CAShapeLayer layer];
		shapeLayer_.name=@"bg";
		shapeLayer_.anchorPoint=CGPointMake(0, 0);
		shapeLayer_.bounds = CGRectMake(0.f, 0.f, 119.f, 119.f); 
		shapeLayer_.lineWidth = 1.f; 
		currentArc = M_PI_2; 
		 rPath = @"View/Album View/dish_oper_layer.png";
		//UIImage *adjustImg = (id)getSkinImg(rPath).CGImage;
		shapeLayer_.fillColor = [UIColor colorWithPatternImage:getSkinImg(rPath)].CGColor;

		//四个按钮弧线 (360/4) = 90 /2 = 45  43 
		CALayer* addLayer =[CALayer layer];
		rPath = @"View/Album View/dish_plus.png";
		addLayer.bounds = CGRectMake(0.f, 0.f, 29.f, 29.f);
		addLayer.position = CGPointMake(21, 58);
		addLayer.contents =(id)getSkinImg(rPath).CGImage;
		[shapeLayer_ addSublayer:addLayer];
		
		CALayer* miunsLayer =[CALayer layer];
		rPath = @"View/Album View/dish_miuns.png";
		miunsLayer.bounds = CGRectMake(0.f, 0.f, 29.f, 29.f);
		miunsLayer.position = CGPointMake(100, 58);
		miunsLayer.contents =(id)getSkinImg(rPath).CGImage;
		[shapeLayer_ addSublayer:miunsLayer];
		
		CALayer* detailLayer =[CALayer layer];
		rPath =aType==DishOper_Album? @"View/Album View/dish_oper_detail.png":@"View/Album View/dish_oper_album.png";
		detailLayer.bounds = CGRectMake(58.f, 10.f, 40.f, 22.f);
		detailLayer.position = CGPointMake(58.f, 20);
		detailLayer.contents =(id)getSkinImg(rPath).CGImage;
		[shapeLayer_ addSublayer:detailLayer];
		
		CALayer* deleteLayer =[CALayer layer];
		rPath = @"View/Album View/oper_clean.png";
		deleteLayer.bounds = CGRectMake(58.f, 88.f, 40.f, 22.f);
		deleteLayer.position = CGPointMake(58.f, 95);
		deleteLayer.contents =(id)getSkinImg(rPath).CGImage;
		[shapeLayer_ addSublayer:deleteLayer];
 
		if (!aType==DishOper_Album) { 
			CALayer* closeLayer =[CALayer layer];
			rPath = @"View/Album View/oper_close.png";
			closeLayer.bounds = CGRectMake(58.f, 88.f, 38.f, 22.f);
			closeLayer.position = CGPointMake(59, 59);
			closeLayer.contents =(id)getSkinImg(rPath).CGImage;
			[shapeLayer_ addSublayer:closeLayer];
		} 
		
		[self.layer addSublayer:shapeLayer_];
		[self drawPathWithArc:currentArc];
		
		CADisplayLink *displayLink = [CADisplayLink displayLinkWithTarget:self selector:@selector(updatePath:)];
		[displayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];
 
	}
	return self;
} 
 
#pragma mark View drawing 
- (void)drawLayer:(CAShapeLayer*)aLayer arc:(CGFloat)arc {
	CGMutablePathRef thePath = CGPathCreateMutable();
	CGPathMoveToPoint(thePath, NULL, shapeLayer_.bounds.size.width/2, shapeLayer_.bounds.size.height/2);
	CGPathAddLineToPoint(thePath, NULL, 102.f, shapeLayer_.bounds.size.height/5-12);
	CGPathAddArc(thePath, NULL,
				 shapeLayer_.bounds.size.width/2,
				 shapeLayer_.bounds.size.height/2, 
				 shapeLayer_.bounds.size.width/2,
				 0, M_PI/4.0, NO);
	CGPathCloseSubpath(thePath);
	aLayer.path = thePath;
	CGPathRelease(thePath);
}

- (void)drawPathWithArc:(CGFloat)arc {
	CGMutablePathRef thePath = CGPathCreateMutable();
	CGPathMoveToPoint(thePath, NULL, shapeLayer_.bounds.size.width/2, shapeLayer_.bounds.size.height/2);
	
	CGPathAddLineToPoint(thePath, NULL, 119.f, shapeLayer_.bounds.size.height/2);
	CGPathAddArc(thePath, NULL,  shapeLayer_.bounds.size.width/2, shapeLayer_.bounds.size.height/2, shapeLayer_.bounds.size.width/2, 0.f, arc, NO);
	CGPathCloseSubpath(thePath);
	shapeLayer_.path = thePath;
	CGPathRelease(thePath);
}
 
- (void)updatePath:(CADisplayLink *)displayLink {
	currentArc = fminf(currentArc * 1.1f, M_PI * 2.0f);
	[self drawPathWithArc:currentArc];
	if(currentArc >= M_PI * 2.0f) {
		[displayLink invalidate];
	}
}
 
- (void)handleTapFrom:(UITapGestureRecognizer *)recognizer {
	CGPoint location = [recognizer locationInView:self];
	location= CGPointMake(location.x-58.5, location.y-58.5);
	DebugLog(@"进来了已经。。。。。。。");
	NSInteger	rInner = 20;
	//NSInteger	rOuter = 60;
	DishOperViewOperType curType = DishOperView_close;
	
	if ( pow(location.x,2) + pow(location.y,2) < pow(rInner,2)) {
		//curType = DishOperView_close;
		//通知主界面让TopBar在最顶层
		[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_ButtonClicked.mp3"]];
		postNWithObj(kMessage_DishMain_SwitchTopBarFront,self);
		[self removeFromSuperview];
	}
	else {
		 
		float_t   angle   = atan2(location.y-0, location.x-0);
		DebugLog(@"角度：%f",abs(angle)); 
		if ( angle >2.30f ||  angle < -2.4f  ) {//增加2.319174  -2.395136
			DebugLog(@"增加一份");
			[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_ButtonClicked.mp3"]];
			curType= DishOperView_Plus;
		}
		else if (abs(angle) < 0.68f ) {//减少 -0.677386  0.774163
			[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_ButtonClicked.mp3"]];
			DebugLog(@"减少一份");
			curType= DishOperView_Miuns;
		}
		else if (angle > 0.84f && angle < 2.3f ) {//删除
			[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_ButtonClicked.mp3"]];
			DebugLog(@"删除全部--需要确认");
			curType= DishOperView_Delete;
		}
		else if (angle > -2.3f && angle < -0.7f) {//详细
			DebugLog(@"查看详细详细");
			[APPSESSION playSoundOnEvent:[XRmenuPublic getGeneralSkinImg:@"View/A_Flips.mp3"]];
			curType= DishOperView_Detail;
		}
		if (self.delegate) {
			[self.delegate dishOperView:self didTappedWith:curType];
		}
	}
}

@end
